package com.demo.controller;


import com.demo.entity.MedicineCate;
import com.demo.response.Result;
import com.demo.service.IMedicineCateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 预防药物类目（型） 前端控制器
 * @author ZM
 * @since 2020-11-20
 */
@Api(tags = "预防药物类目（型）")
@RestController
@RequestMapping("/medicineCate")
@Slf4j
public class MedicineCateController {
    @Autowired
    IMedicineCateService cateService;

    @ApiOperation("新增预防药物类目（型）")
    @PostMapping("/addMedicineCate")
    public Result addMedicineCate(@Validated @RequestBody MedicineCate medicineCate, BindingResult result){
        if (result.hasErrors()){
            return Result.error(result.getFieldError().getDefaultMessage());
        }
        return cateService.addMedicineCate(medicineCate);
    }

    @ApiOperation("编辑预防药物类目（型）")
    @PostMapping("/updateMedicineCate")
    public Result updateMedicineCate(@RequestBody MedicineCate medicineCate, BindingResult result){
        if (result.hasErrors()){
            return Result.error(result.getFieldError().getDefaultMessage());
        }
        return cateService.updateMedicineCate(medicineCate);
    }

    @ApiOperation("删除预防药物类目（型）")
    @GetMapping("/deleteMedicineCate")
    public Result deleteMedicineCate(@RequestParam(name = "medicineCateId",required = true)Integer medicineCateId,
                                     @RequestParam(name = "deletedUserId",required = true)Integer deletedUserId){
        return cateService.deleteMedicineCate(medicineCateId, deletedUserId);
    }

    @ApiOperation("预防药物类目（型）列表")
    @GetMapping("/medicineCateList")
    public Result medicineCate(@RequestParam(name = "pageNumber",required = false,defaultValue = "1")Integer pageNumber,
                               @RequestParam(name = "pageSize",required = false,defaultValue = "10")Integer pageSize,
                               @RequestParam(name = "medicineCateId",required = false)Integer medicineCateId,
                               @RequestParam(name = "category",required = false)String category){
        return cateService.medicineCateList(pageNumber, pageSize, medicineCateId,category);
    }



}
